$(function() {
	var template = '<div class="modal fade" id="suredyModal" tabindex="-1" role="dialog" aria-labelledby="suredyModalLabel" aria-hidden="true">\
		<div class="modal-dialog">\
			<div class="modal-content" style="border-radius: 0;">\
				<div class="modal-header" style="background-color: #337ab7; color: #ddd; min-height: 56px;">\
					<button type="button" class="close" data-dismiss="modal" aria-label="Close">\
						<span aria-hidden="true" class="icon-remove"></span>\
					</button>\
					<h4 class="modal-title" id="suredyModalLabel"></h4>\
				</div>\
				<div class="modal-body"></div>\
				<div class="modal-footer" style="padding: 10px 15px;">\
					<button type="button" class="btn btn-primary btn-ok"></button>\
					<button type="button" class="btn btn-default btn-cancel" data-dismiss="modal"></button>\
				</div>\
			</div>\
		</div>\
	</div>';

	if ($('body').has('#suredyModal').length <= 0)
		$('body').append(template);

	$('#suredyModal').on('hidden.bs.modal', function(e) {
		// unbind click event
		$('.modal-footer .btn-ok', '#suredyModal').unbind('click');
		// reset all options
		$.suredy.modal.create();
	});
});

(function($) {
	$.suredy = $.extend({}, $.suredy);

	$.suredy.modal = {
		create : function(options) {
			var opts = $.extend({}, {
				size : 'normal',// lg sm normal
				showHead : true,
				showFoot : true,
				showBody : true,
				icon : 'icon-exclamation-sign',
				title : '系统消息',
				showOkBtn : true,
				showCancelBtn : true,
				okText : '确定',
				cancelText : '关闭',
				text : '系统消息',
				uri : '',
				data : {},
				okClick : null
			}, options);

			var modal = $('#suredyModal');

			if (modal.length <= 0) {
				alert('Can not find suredy modal.');
				return;
			}

			// set window size
			if (opts.size === 'lg')
				$('.modal-dialog', modal).addClass('modal-lg');
			else if (opts.size === 'sm')
				$('.modal-dialog', modal).addClass('modal-sm');
			else {
				$('.modal-dialog', modal).removeClass('modal-lg');
				$('.modal-dialog', modal).removeClass('modal-sm');
			}

			// set header
			if (opts.showHead) {
				$('.modal-header', modal).removeClass('hidden');

				// set icon
				$('#suredyModalLabel', modal).html('<i class="' + opts.icon + '" style="margin-right: 10px;"></i>' + opts.title);
			} else
				$('.modal-header', modal).addClass('hidden');

			// set footor
			if (opts.showFoot) {
				$('.modal-footer', modal).removeClass('hidden');

				// set ok btn
				if (opts.showOkBtn) {
					$('.modal-footer .btn-ok', modal).removeClass('hidden');
					$('.modal-footer .btn-ok', modal).text(opts.okText);
				} else
					$('.modal-footer .btn-ok', modal).addClass('hidden');

				// set cancel btn
				if (opts.showCancelBtn) {
					$('.modal-footer .btn-cancel', modal).removeClass('hidden');
					$('.modal-footer .btn-cancel', modal).text(opts.cancelText);
				} else
					$('.modal-footer .btn-cancel', modal).addClass('hidden');
			} else
				$('.modal-footer', modal).addClass('hidden');

			// set content
			if (opts.showBody) {
				$('.modal-content', modal).removeClass('hidden');
				if (opts.uri && typeof opts.uri === 'string' && opts.uri != '')
					// load content from service
					$.suredy.loadContent(opts.uri, $('.modal-body', modal), opts.data);
				else
					$('.modal-body', modal).text(opts.text);
			} else {
				$('.modal-content', modal).addClass('hidden');
			}

			// bind click event
			if (opts.okClick)
				$('.modal-footer .btn-ok', modal).bind('click', opts.okClick);

			return modal;
		}
	};

	$.suredy.showModal = function(data) {
		this.modal.create(data).modal();
	};

	$.suredy.closeModal = function() {
		$('#suredyModal').modal('hide');
	};
})(jQuery);
